package cn.edu.scau.cmi.ema.dao;

import cn.edu.scau.cmi.ema.base.CmiPagination;
import cn.edu.scau.cmi.ema.domain.Foodmaterial;
import cn.edu.scau.cmi.ema.util.CmiSqlUtil;
import org.hibernate.Session;
import org.hibernate.criterion.DetachedCriteria;
import org.springframework.dao.DataAccessException;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

import javax.persistence.EntityManager;
import javax.persistence.NoResultException;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;
import java.math.BigDecimal;
import java.util.Arrays;
import java.util.HashSet;
import java.util.LinkedHashSet;
import java.util.Set;

@Repository("FoodmaterialDAO")
@Transactional
public class FoodmaterialDAOImpl extends AbstractJpaDao<Foodmaterial> implements FoodmaterialDAO {

	private final static Set<Class<?>> dataTypes = new HashSet<Class<?>>(
			Arrays.asList(new Class<?>[] { Foodmaterial.class }));

	@PersistenceContext()
	private EntityManager entityManager;

	public FoodmaterialDAOImpl() {
		super();
	}

	public EntityManager getEntityManager() {
		return entityManager;
	}

	public Set<Class<?>> getTypes() {
		return dataTypes;
	}

	@Transactional
	public Foodmaterial findFoodmaterialById(Integer id) throws DataAccessException {

		return findFoodmaterialById(id, -1, -1);
	}

	@Transactional
	public Foodmaterial findFoodmaterialById(Integer id, int startResult, int maxRows) throws DataAccessException {
		try {
			Query query = createNamedQuery("findFoodmaterialById", startResult, maxRows, id);
			return (Foodmaterial) query.getSingleResult();
		} catch (NoResultException nre) {
			return null;
		}
	}

	@Transactional
	public Set<Foodmaterial> findFoodmaterialByName(String name) throws DataAccessException {

		return findFoodmaterialByName(name, -1, -1);
	}

	@SuppressWarnings("unchecked")
	@Transactional
	public Set<Foodmaterial> findFoodmaterialByName(String name, int startResult, int maxRows)
			throws DataAccessException {
		Query query = createNamedQuery("findFoodmaterialByName", startResult, maxRows, name);
		return new LinkedHashSet<Foodmaterial>(query.getResultList());
	}

	@Transactional
	public Set<Foodmaterial> findFoodmaterialByNameContaining(String name) throws DataAccessException {

		return findFoodmaterialByNameContaining(name, -1, -1);
	}

	@SuppressWarnings("unchecked")
	@Transactional
	public Set<Foodmaterial> findFoodmaterialByNameContaining(String name, int startResult, int maxRows)
			throws DataAccessException {
		Query query = createNamedQuery("findFoodmaterialByNameContaining", startResult, maxRows, name);
		return new LinkedHashSet<Foodmaterial>(query.getResultList());
	}

	@Transactional
	public Set<Foodmaterial> findFoodmaterialByContent(BigDecimal content) throws DataAccessException {

		return findFoodmaterialByContent(content, -1, -1);
	}

	@SuppressWarnings("unchecked")
	@Transactional
	public Set<Foodmaterial> findFoodmaterialByContent(BigDecimal content, int startResult, int maxRows)
			throws DataAccessException {
		Query query = createNamedQuery("findFoodmaterialByContent", startResult, maxRows, content);
		return new LinkedHashSet<Foodmaterial>(query.getResultList());
	}

	@Transactional
	public Set<Foodmaterial> findFoodmaterialByDescription(String description) throws DataAccessException {

		return findFoodmaterialByDescription(description, -1, -1);
	}

	@SuppressWarnings("unchecked")
	@Transactional
	public Set<Foodmaterial> findFoodmaterialByDescription(String description, int startResult, int maxRows)
			throws DataAccessException {
		Query query = createNamedQuery("findFoodmaterialByDescription", startResult, maxRows, description);
		return new LinkedHashSet<Foodmaterial>(query.getResultList());
	}

	@Transactional
	public Set<Foodmaterial> findFoodmaterialByDescriptionContaining(String description) throws DataAccessException {

		return findFoodmaterialByDescriptionContaining(description, -1, -1);
	}

	@SuppressWarnings("unchecked")
	@Transactional
	public Set<Foodmaterial> findFoodmaterialByDescriptionContaining(String description, int startResult, int maxRows)
			throws DataAccessException {
		Query query = createNamedQuery("findFoodmaterialByDescriptionContaining", startResult, maxRows, description);
		return new LinkedHashSet<Foodmaterial>(query.getResultList());
	}

	@Transactional
	public Foodmaterial findFoodmaterialByPrimaryKey(Integer id) throws DataAccessException {
		return findFoodmaterialByPrimaryKey(id, -1, -1);
	}

	@Transactional
	public Foodmaterial findFoodmaterialByPrimaryKey(Integer id, int startResult, int maxRows)
			throws DataAccessException {
		try {
			Query query = createNamedQuery("findFoodmaterialByPrimaryKey", startResult, maxRows, id);
			return (Foodmaterial) query.getSingleResult();
		} catch (NoResultException nre) {
			return null;
		}
	}

	@Transactional
	public Set<Foodmaterial> findFoodmaterialsByIds(Set<Integer> ids) throws DataAccessException {
		try {
			Query query = createNamedQuery("findFoodmaterialsByIds", -1, -1, ids);
			return new HashSet(query.getResultList());
		} catch (NoResultException nre) {
			return null;
		}
	}

	@Transactional
	public Set<Foodmaterial> findAllFoodmaterials() throws DataAccessException {

		return findAllFoodmaterials(-1, -1);
	}

	@SuppressWarnings("unchecked")
	@Transactional
	public Set<Foodmaterial> findAllFoodmaterials(int startResult, int maxRows) throws DataAccessException {
		Query query = createNamedQuery("findAllFoodmaterials", startResult, maxRows);
		return new LinkedHashSet<Foodmaterial>(query.getResultList());
	}

	@SuppressWarnings("unchecked")
	public Set<Foodmaterial> findFoodmaterialsByFood(Integer foodId, int start, int max) throws DataAccessException {
		Query query = createNamedQuery("findAllFoodmaterialsByFood", start, max, foodId);
		return new LinkedHashSet<Foodmaterial>(query.getResultList());
	}

	@SuppressWarnings("unchecked")
	public Set<Foodmaterial> findFoodmaterialsByMaterial(Integer materialId, int start, int max)
			throws DataAccessException {
		Query query = createNamedQuery("findAllFoodmaterialsByMaterial", start, max, materialId);
		return new LinkedHashSet<Foodmaterial>(query.getResultList());
	}

	@SuppressWarnings("unchecked")
	@Transactional
	public Set<Foodmaterial> findFoodmaterialByCompositeProperities(String sql, int startResult, int maxRows) {
		return new LinkedHashSet<Foodmaterial>(createQuery(sql, startResult, maxRows).getResultList());
	}

	@Transactional
	public Set<Foodmaterial> findFoodmaterialsByDetachedCriteria(CmiPagination page) throws DataAccessException {
		DetachedCriteria detachedCriteria = CmiSqlUtil.createDetachedCriteria(page, Foodmaterial.class);
		Session session = (Session) entityManager.getDelegate();
		Set<Foodmaterial> foodmaterials = new HashSet(detachedCriteria.getExecutableCriteria(session)
				.setFirstResult(page.getSearchBeginCursor()).setMaxResults(page.getPaginationSize()).list());

		return foodmaterials;
	}

	@Transactional
	public Foodmaterial findFoodmaterialByPhysicalPrimaryKey(Integer foodId, Integer materialId, BigDecimal content)
			throws DataAccessException {
		return findFoodmaterialByPhysicalPrimaryKey(foodId, materialId, content, -1, -1);
	}

	@Transactional
	public Foodmaterial findFoodmaterialByPhysicalPrimaryKey(Integer foodId, Integer materialId, BigDecimal content,
			int startResult, int maxRows) throws DataAccessException {
		try {
			Query query = createNamedQuery("findFoodmaterialByPhyscialPrimaryKey", startResult, maxRows, foodId,
					materialId, content);
			return (Foodmaterial) query.getSingleResult();
		} catch (NoResultException nre) {
			return null;
		}
	}

	public boolean canBeMerged(Foodmaterial entity) {
		return true;
	}
}
